package demo0910;

public class T2_0520 {
    private static Object locker = new Object();
    private static int num = 3;
    public static void main(String[] args) {
        Thread thread1 = new Thread("a") {
            @Override
            public void run() {
                synchronized (locker) {
                    while (num != 1) {
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println(Thread.currentThread().getName());
                    locker.notifyAll();
                    num = 2;
                }

            }
        };
        Thread thread2 = new Thread("b") {
            @Override
            public void run() {
                synchronized (locker) {
                    while (num != 2) {
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println(Thread.currentThread().getName());
                    locker.notifyAll();
                    num = 3;
                }

            }
        };
        Thread thread3 = new Thread("c") {
            @Override
            public void run() {
                synchronized (locker) {
                    while (num != 3) {
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println(Thread.currentThread().getName());
                    locker.notifyAll();
                    num = 1;
                }


            }
        };

        thread1.start();
        thread2.start();
        thread3.start();
    }
}
